/*
https://leetcode-cn.com/problems/image-overlap/solution/tu-xiang-zhong-die-by-leetcode/
 */
import java.util.*;

public class Solution835 {
    public int largestOverlap(int[][] img1, int[][] img2) {
        Set<List<Integer>> set1=new HashSet<>();
        Set<List<Integer>> set2=new HashSet<>();
        Map<List<Integer>,Integer> map=new HashMap<>();
        for (int i=0;i<img1.length;i++){
            for (int j=0;j<img1.length;j++){
                if (img1[i][j]==1){
                    set1.add(Arrays.asList(i,j));
                }
            }
        }
        for (int i=0;i<img1.length;i++){
            for (int j=0;j<img1.length;j++){
                if (img2[i][j]==1){
                    set2.add(Arrays.asList(i,j));
                }
            }
        }
        int ans=0;
        for (List<Integer> l1 : set1) {
            for (List<Integer> l2 : set2) {
                List<Integer> list=Arrays.asList(l1.get(0)-l2.get(0),l1.get(1)-l2.get(1));
                map.put(list,map.getOrDefault(list,0)+1);
                ans=Math.max(ans,map.get(list));
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution835().largestOverlap(new int[][]{{1,1,0},{0,1,0},{0,1,0}},new int[][]{{0,0,0},{0,1,1},{0,0,1}}));
    }
}
